//
//  WBPicViewerController.swift
//  WeiboTen
//
//  Created by ItHeiMa on 2017/7/15.
//  Copyright © 2017年 itHeima. All rights reserved.
//

import UIKit
import SDWebImage

class WBPicViewerController: UIViewController {
    
    lazy var scrollView: UIScrollView = {
        let sc = UIScrollView(frame: UIScreen.main.bounds)
        sc.delegate = self
        
        //允许缩放
        sc.minimumZoomScale = 0.5
        sc.maximumZoomScale = 2
        
        return sc
    }()
    
    lazy var imageView: UIImageView = UIImageView()
    
    var currentIndex: Int
    var picModel: WBStatusPicModel
    
    //构造函数的重载，swift的类默认有一个init()方法，一旦我们自己定义了其它的init方法则默认的init方法就失效了
    init(currentIndex: Int, picModel: WBStatusPicModel) {
        //先保证自己的存储属性被正确初始化
        self.currentIndex = currentIndex
        self.picModel = picModel
        
        //子类必须要调用父类的指定构造器，保证父类的存储属性被正确初始化
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.black
        
        self.setupUI()
    }
}

extension WBPicViewerController {
    func setupUI () {
        self.view.addSubview(scrollView)
        scrollView.addSubview(imageView)
        
        let url = URL(string: picModel.big_pic!)
        
        SDWebImageManager.shared.loadImage(with: url, options: [], progress: nil, completed: { (downloadImage, _, _, _,_,_) in
            
            if let downloadImage = downloadImage {
                //最小高度
                let minHeight: CGFloat = 150
                
                //拿到图片的原始的宽度
                let imageSize = downloadImage.size
                
                //1.先把图片的宽度调整成和屏幕的宽度一至
                let newHeight = screenWidth * imageSize.height / imageSize.width
                var newSize = CGSize(width: screenWidth, height: newHeight)

                //如果图片过窄，就把图片调到150高度
                if newSize.height < 150 {
                    let newWidth = minHeight * newSize.width / newSize.height
                    newSize.height = minHeight
                    newSize.width = newWidth
                }
                
                //2. 判断图片的高度是否大于屏幕的高度
                if newSize.height > screenHeight {
                    self.imageView.frame = CGRect(origin: CGPoint.zero, size: newSize)
                    self.scrollView.contentSize = newSize
                } else if newSize.height <= screenHeight && newSize.width == screenWidth {
                    self.imageView.frame = CGRect(origin: CGPoint.zero, size: newSize)
                    self.imageView.center = self.scrollView.center
                } else if newSize.height <= screenHeight && newSize.height == 150 {
                    self.imageView.frame = CGRect(x: 0, y: screenHeight/2-minHeight/2, width: newSize.width, height: newSize.height)
                    self.scrollView.contentSize = CGSize(width: newSize.width, height: 0)
                }
                //设置图片
                self.imageView.image = downloadImage
            }
        })
    }
}

extension WBPicViewerController: UIScrollViewDelegate {
    func scrollViewDidZoom(_ scrollView: UIScrollView) {
        //获取缩放比例
    }
}
